package naga.x.platform2.channel.q724.support;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import naga.x.platform2.utils.CommonUtils;
import naga.x.platform2.utils.IOUtils;

/**
 * 7724
 * @see <a href="http://open.7724.com/">http://open.7724.com/</a>
 * @author yang.li
 *
 */
public class Q724Api {

	public static String makeSign(Map<String, String> map, String secKey, boolean encodeUrl) {
		Map<String, String> tree = map instanceof TreeMap ? map : new TreeMap<String, String>(map);
		StringBuilder buf = new StringBuilder(128);
		for (Entry<String, String> entry : tree.entrySet()) {
			String key = entry.getKey();
			String val = entry.getValue();
			buf.append(entry.getKey()).append('=').append(val).append('&');
			if (encodeUrl) {
				val = CommonUtils.encodeURL(val, IOUtils.UTF8);
				if (tree == map) {
					entry.setValue(val);
				} else {
					map.put(key, val);
				}
			}
		}
		buf.append(secKey);
		return CommonUtils.md5(buf.toString());
	}
	
}
